package com.lgk.controller;

import com.github.pagehelper.PageInfo;
import com.lgk.domain.Role;
import com.lgk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * go to 角色界面
     * @param model
     * @param username
     * @return
     */
    @RequestMapping("/to/roleList/{username}")
    public String toRole(Model model, @PathVariable("username")String username){
        return getString(model, username);
    }

    /**
     * 封装一个公共数据和跳转用户页
     * @param model
     * @param username
     * @return
     */
    private String getString(Model model, @PathVariable("username") String username) {
        PageInfo<Role> pageRole = roleService.getPageRole(1);
        model.addAttribute("page", pageRole);
        model.addAttribute("username", username);
        model.addAttribute("isGetPage", true);
        model.addAttribute("isGetRolePage", false);
        model.addAttribute("searchRoleName","");
        return "role_list";
    }

    /**
     * 分页查询
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/getRolePage/{pageNum}/{username}", method = RequestMethod.GET)
    public String getPageRole(@PathVariable("pageNum")Integer pageNum, Model model, @PathVariable("username") String username) {
        //获取分页结果
        PageInfo<Role> page = roleService.getPageRole(pageNum);
        //放入请求狱中
        model.addAttribute("page", page);
        model.addAttribute("username", username);
        model.addAttribute("isGetPage", true);
        model.addAttribute("isGetRolePage", false);
        model.addAttribute("searchRoleName","");
        return "role_list" ;
    }

    /**
     * 添加角色
     * @param role
     * @param model
     * @return
     */
    @RequestMapping("/addRole/{username}")
    public String addRole(Role role, Model model, @PathVariable("username") String username){
        roleService.addRole(role);
        return getString(model, username);
    }

    /**
     * 修改角色
     * @param model
     * @param role
     * @return
     */
    @RequestMapping("/updateRole/{username}")
    public String updateRole(Model model, Role role, @PathVariable("username") String username){
        roleService.updateRole(role);
        return getString(model, username);
    }

    /**
     * 根据id查询角色
     * @param id
     */
    @RequestMapping(value = "/getRole/{id}/{username}",method = RequestMethod.GET)
    public String getById(@PathVariable("username")String username, @PathVariable("id")Integer id, Model model) {
        Role changeRole = roleService.getById(id);
        model.addAttribute("changeRole", changeRole);
        model.addAttribute("username", username);
        return "role_update";
    }

    /**
     * 删除角色
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/deleteRole/{id}/{username}")
    public String deleteRole(@PathVariable("id")Integer id, Model model, @PathVariable("username") String username) {
        roleService.deleteRole(id);
        return getString(model, username);
    }

    /**
     * 根据名字模糊分页查询
     * @param pageNum
     * @param role
     * @param model
     * @param username
     * @return
     */
    @RequestMapping("/searchRole/{username}/{pageNum}")
    public String getPageByName(@PathVariable("pageNum") Integer pageNum, Role role, Model model, @PathVariable("username") String username) {
        PageInfo<Role> pageRole = roleService.getPageByName(role.getName(), pageNum);
        model.addAttribute("page", pageRole);
        model.addAttribute("username", username);
        model.addAttribute("isGetPage", false);
        model.addAttribute("isGetUserPage", true);
        model.addAttribute("searchRoleName",role.getName());
        return "role_list";
    }
}
